package com.glacier.yuyuan.gen.webapp.web;

import cn.hutool.core.util.StrUtil;
import com.glacier.common.core.domain.Page;
import com.glacier.common.core.domain.Result;
import com.glacier.common.core.domain.form.ListWrapper;
import com.glacier.yuyuan.gen.webapp.domain.GenDatasource;
import com.glacier.yuyuan.gen.webapp.domain.GenTableInfo;
import com.glacier.yuyuan.gen.webapp.service.GenDatasourceService;
import com.glacier.yuyuan.gen.webapp.service.GenTableInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * date 2022-11-10 15:12
 * 表信息 控制层
 *
 * @author glacier
 * @version 1.0
 */
@RequestMapping("/genTableInfo")
@Controller
public class GenTableInfoController extends BaseController {

    private GenTableInfoService genTableInfoService;

    private GenDatasourceService genDatasourceService;

    @Autowired
    public void setGenTableInfoService(GenTableInfoService genTableInfoService) {
        this.genTableInfoService = genTableInfoService;
    }

    @Autowired
    public void setGenDatasourceService(GenDatasourceService genDatasourceService) {
        this.genDatasourceService = genDatasourceService;
    }

    @GetMapping("")
    public String list(Model model) {
        // 查询所有数据源
        model.addAttribute("dataSources", genDatasourceService.findList(new GenDatasource()));
        return "view/genTableInfo/genTableInfoList";
    }

    @ResponseBody
    @PostMapping("/page")
    public Result<List<GenTableInfo>> page(Page<GenTableInfo> page, GenTableInfo genTableInfo) {
        page = genTableInfoService.findPage(page, genTableInfo);
        return Result.ok("ok", page.getData(), page.getTotal());
    }

    @GetMapping("/add")
    public String add(GenTableInfo genTableInfo, Model model) {
        // 查询所有数据源
        model.addAttribute("dataSources", genDatasourceService.findList(new GenDatasource()));
        model.addAttribute("entity", genTableInfo);
        return "view/genTableInfo/genTableInfoAdd";
    }

    @GetMapping("/form")
    public String form(GenTableInfo genTableInfo, Model model) {
        if (StrUtil.isNotBlank(genTableInfo.getId())) {
            genTableInfo = genTableInfoService.findById(genTableInfo.getId());
        } else {
            genTableInfo = genTableInfoService.getGenTableInfo(
                    genTableInfo.getSourceId(), genTableInfo.getTableName());
        }
        model.addAttribute("entity", genTableInfo);
        return "view/genTableInfo/genTableInfoForm";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result<Integer> save(GenTableInfo genTableInfo) {
        return Result.ok("操作成功！", genTableInfoService.save(genTableInfo));
    }

    /**
     * 保存并生成代码
     *
     * @param genTableInfo
     * @return
     */
    @PostMapping("/code")
    @ResponseBody
    public Result<Integer> code(GenTableInfo genTableInfo) {
        return Result.ok("操作成功！", genTableInfoService.saveAndGenCode(genTableInfo));
    }

    @ResponseBody
    @PostMapping("/del")
    public Result<String> del(ListWrapper<String> form) {
        genTableInfoService.batchDelete(form.getDatas());
        return Result.ok("操作成功", "");
    }
}
